<?php

/**
 * 默认控制器
 */
class Controller_Default extends Controller_Abstract
{
	/**********/
	function actionUserwel(){
		
		if ($this->_app->currentUserRoles())
        {
            $show_box['title'] = '快速启动　　';

			Operate::everyOperate($_SESSION['acl_task_userdata']['account'],'',$_SESSION['acl_task_userdata']['group_name'],''.$show_box['title'],'');

			$this->_view['form'] = new Form_OpSearch(url('default::default/serverlog'));
			$this->_view['form']->_subject = '按日期查询　';
			//$this->_view['title'] = '首页-当前状态';
			$this->_view['nowuser']= $_SESSION['acl_task_userdata']['account'];
			$this->_view['nowgroup']= $_SESSION['acl_task_userdata']['group_name'];
        }
        else
        {
            // 未登录则转到登录页面
            return $this->_redirect(url('default/login'));
        }
	}

	function actionToptenlog(){
		
		if(!$this->_app->currentUserRoles())
			return $this->_redirect(url('default/login'));
		
		

		$show_box['title'] = '最新报警记录';
		//dump($_SESSION);
		Operate::everyOperate($_SESSION['acl_task_userdata']['account'],'',$_SESSION['acl_task_userdata']['group_name'],''.$show_box['title'],'');
		$this->_view['title'] = '最新报警记录';
		$addconds='';$addconds1='';
		$this->_view['startd']='';
		$this->_view['endd']='';
		$this->_view['countno'] = 1;$this->_view['countno1'] = 1;
		if ($this->_context->isPOST())
		{
				if($_POST['startdate']==$_POST['enddate']){
					$endd=$this->my_date_add('d',$_POST['enddate'],1);
				}else{
					$endd=$_POST['enddate'];
				}
				$this->_view['startd']=$_POST['startdate'];
				$this->_view['endd']=$endd;
				$addconds="time >='".$_POST['startdate']."' and time<='".$endd."'";
				$addconds1="date >='".$_POST['startdate']."' and date<='".$endd."'";
				//echo $this->my_date_add('d',$_POST['enddate'],1);
				//echo $addconds;
				//dump($_POST);
		}

		if($this->_context->startd !='' && $this->_context->endd !=''){
			$this->_view['startd']=$this->_context->startd;
			$this->_view['endd']=$this->_context->endd;
			$addconds="time >='".$this->_context->startd."' and time<='".$this->_context->endd."'";
			$addconds1="date >='".$this->_context->startd."' and date<='".$this->_context->endd."'";
		}
		
		$select = EventAlarm::find($addconds)->limit(0, 10);
		$this->_view['group'] = $select->order("id desc")->getAll();

		$select1 = AlarmTable::find($addconds1)->limit(0, 10);
		$this->_view['group1'] = $select1->order("id desc")->getAll();
		
	}

	/****接收邮箱******/
	function actionRemailing(){
		
		if(!$this->_app->currentUserRoles())
			return $this->_redirect(url('default/login'));

		$this->_view['form'] = new Form_TheMail(url('default::default/remailing'));
		
		//添加
		if($this->_context->isPOST() && $this->_view['form']->validate($_POST)){
			try{
				if($_POST['hid']==''){
					// 查询指定用户 ID
					$edit_one = TheMail::find('mail = ?', $_POST['mail'])->query();
					if (!$edit_one->id())
					{	
						try{
							$newgroup = new TheMail($_POST);
							//print_r($form->values());
							$newgroup->save();

							return $this->_redirect(url('default::default/remailing'));
						}catch (QDB_ActiveRecord_ValidateFailedException $ex){
							$this->_view['form']->invalidate($ex);
						}
						// 如果用户 ID 无效，则返回用户管理首页
						//return $this->_redirect(url('default::rulesetting/index'));
					}else{
						//return '　';
						return $this->_redirect(url('default::default/remailing'));
					}
				}else{
					
					try{
						$edit_one = TheMail::find('id = ?', $_POST['hid'])->query();
						$edit_one->changeProps($_POST);
						// 保存并重定向浏览器
						$edit_one->save();
						
						return $this->_redirect(url('default::default/remailing'));
					}catch (QDB_ActiveRecord_ValidateFailedException $ex){
						$this->_view['form']->invalidate($ex);
					}
					//dump('ass');
				}

			}catch(QValidation_FailedException $ex){
				$this->_view['form']->invalidate($ex);
				return $this->_redirect(url('default::default/remailing'));
			}
		}else{
			//dump($this->_context->id);
			if(isset($this->_context->id)){
				list($id, $go) = explode('|', $this->_context->id);
				//dump($go);
				if($go=='edit'){
					$edit_one = TheMail::find('id = ?',$id)->query();
					if (!$edit_one->id())
					{
						return $this->_redirect(url('default::default/remailing'));
					}else{
						//dump($edit_one);
						$this->_view['form']->import($edit_one);
						$this->_view['form']['hid']->set('value',$id);
						//dump('开始修改s');
					}
				}else if($go=='del'){
					$rid = $id;
					TheMail::meta()->destroyWhere('id = ?', $rid);
					if(mysql_affected_rows())
					{
//						return $this->_redirectMessage(
//						'删除成功', 
//						'指定设置删除成功',
//						url('default::default/framework', array('control' => 'default','acts' => 'remailing|userwel')));
					}
					else
					{
						return $this->_redirectMessage(
						'删除失败', 
						'指定设置删除失败',
						url('default::default/framework', array('control' => 'default','acts' => 'remailing|userwel')));
					}
				}
			}
		}

		$show_box['title'] = '连接状态';
		$this->_view['title'] = '连接状态';

		Operate::everyOperate($_SESSION['acl_task_userdata']['account'],'',$_SESSION['acl_task_userdata']['group_name'],''.$show_box['title'],'');

		$addconds='';
		$this->_view['startd']='';
		$this->_view['endd']='';
		$this->_view['countno'] = 1;

		// 设置页面列表数目
		$pagesize = 10;
		// 获取当前页数，通常为get方式参数
		$page = $this->_context->page;
		$this->_view['pageno']=$page;
		$select = TheMail::find()->limitPage($page, $pagesize);
		$this->_view['pager'] = $select->getPagination();
		$this->_view['group'] = $select->getAll();
		//echo $this->_view['pager']['record_count'];
		if($this->_view['pager']['record_count']>0 && $page=='')
			$page=1;

		$pagediv = new Helper_JsPager($this->_view['pager']['record_count'], $this->_view['pager']['page_size'], 9, $page, 'opgopage(\'default|remailing|','\')',5);
		$this->_view['pagestr']= $pagediv->show();
	}

	/****授权信息******/
	function actionLicense(){

		if(!$this->_app->currentUserRoles())
			return $this->_redirect(url('default/login'));
		

		$this->_view['title'] = '授权信息';
		Operate::everyOperate($_SESSION['acl_task_userdata']['account'],'',$_SESSION['acl_task_userdata']['group_name'],''.$this->_view['title'],'');

		$this->_view['countno'] = 1;
		$select = ServerLicense::find()->limit(0, 1);
		$this->_view['group'] = $select->getAll();
		//dump($_SESSION);
	}

	/****服务器邮件通知设置******/
	function actionMailsetting(){

		if(!$this->_app->currentUserRoles())
			return $this->_redirect(url('default/login'));

		$this->_view['title'] = '报警邮箱设置';
		Operate::everyOperate($_SESSION['acl_task_userdata']['account'],'',$_SESSION['acl_task_userdata']['group_name'],''.$this->_view['title'],'');
		
		$this->_view['form'] = new Form_MailSetting(url('default/mailsetting'));
		$this->_view['form']->_subject = $this->_view['title'];
		$edit_one = ServerConfig::find('id=1')->query();
		if ($this->_context->isPOST() && $this->_view['form']->validate($_POST))
        {
			
			if($edit_one->id())
			{
				try{
					$edit_one->changeProps($this->_view['form']->values());
					//$user = new Config($this->_encodeUTF8($this->_view['form']->values()));
					$edit_one->save();
					return '';
				}catch (QDB_ActiveRecord_ValidateFailedException $ex){
					$this->_view['form']->invalidate($ex);
				}
			}else{
				return '　';
			}
        }else{
			//dump('asd');
			//$edit_one = ServerConfig::find('id=1')->query();
			if($edit_one->id())
			{
				$edit_one['mailpasswd']='';
				$this->_view['form']->import($edit_one);
			}
		}
	}

	/**********/
	function actionFramework(){
		
		if(!$this->_app->currentUserRoles())
			return $this->_redirect(url('default/login'));
		
		$show_box['title'] = '框架页面';
		Operate::everyOperate($_SESSION['acl_task_userdata']['account'],'',$_SESSION['acl_task_userdata']['group_name'],''.$show_box['title'],'');
		
		if(isset($this->_context->space)){
			$this->_view['space'] = 'namespace='.$this->_context->space.'&';

			if(isset($this->_context->control)){
				$this->_view['control'] = 'controller='.$this->_context->control.'&';
				
				if(isset($this->_context->acts)){
					$at=split('[|]',$this->_context->acts);
					if(count($at)>1){
						$this->_view['acts'] = 'action='.$at[0].'';
						$this->_view['acts1'] ='action='.$at[1].'';
					}else{
						$this->_view['acts'] = 'action='.$this->_context->acts.'';
						$this->_view['acts1'] ='action=secpart';
					}
				
				}else{
					$this->_view['acts'] ='';
					$this->_view['acts1'] ='action=secpart';
				}
				
			}else
				$this->_view['control'] ='';
		}
		else{
			$this->_view['space']='';
			
			if(isset($this->_context->control)){
				$this->_view['control'] = 'controller='.$this->_context->control.'&';
				
				if(isset($this->_context->acts)){
					
					$at=split('[|]',$this->_context->acts);
					
					if(count($at)>1){
						$this->_view['acts'] = 'action='.$at[0].'';
						$this->_view['acts1'] ='action='.$at[1].'';
					}else{
						$this->_view['acts'] = 'action='.$this->_context->acts.'';
						$this->_view['acts1'] ='action=secpart';
					}
					
					//$this->_view['acts'] = 'action='.$this->_context->acts.'';
					//$this->_view['acts1'] ='action=secpart';
				
				}else{
					$this->_view['acts'] ='';
					$this->_view['acts1'] ='action=secpart';
				}
				
			}else
				$this->_view['control'] ='';
		}
		if(isset($this->_context->user_id))
			$this->_view['userid'] = '&user_id='.$this->_context->user_id;
		else
			$this->_view['userid'] = '';

		if(isset($this->_context->mac))
			$this->_view['userid'] .= '&mac='.$this->_context->mac;

		if(isset($this->_context->flag))
			$this->_view['userid'] .= '&flag='.$this->_context->flag;

		if(isset($this->_context->ipstr))
			$this->_view['userid'] .= '&ipstr='.$this->_context->ipstr;

		if(isset($this->_context->namestr))
			$this->_view['userid'] .= '&namestr='.$this->_context->namestr;

		if(isset($this->_context->group))
			$this->_view['userid'] .= '&groupid='.$this->_context->group;

		if(isset($this->_context->groupid))
			$this->_view['userid'] .= '&groupid='.$this->_context->groupid;

		if(isset($this->_context->id))
			$this->_view['userid'] .= '&id='.$this->_context->id;
	}

	/**********/
	function actionServerlog(){
		
		if(!$this->_app->currentUserRoles())
			return $this->_redirect(url('default/login'));
		
		$show_box['title'] = '系统日志';
		Operate::everyOperate($_SESSION['acl_task_userdata']['account'],'',$_SESSION['acl_task_userdata']['group_name'],''.$show_box['title'],'');
		$this->_view['title'] = '首页-系统日志';
		$addconds='';
		$this->_view['startd']='';
		$this->_view['endd']='';
		$this->_view['logstr']='';
		//search
		$this->_view['form'] = new Form_LogSearch(url('default::default/serverlog'));
		$this->_view['form']->_subject = '按日期查询　';
		//$this->_view['title'] = '首页-当前状态';
		$this->_view['nowuser']= $_SESSION['acl_task_userdata']['account'];
		$this->_view['nowgroup']= $_SESSION['acl_task_userdata']['group_name'];

		if ($this->_context->isPOST())
		{
				if($_POST['startdate']==$_POST['enddate']){
					$endd=$this->my_date_add('d',$_POST['enddate'],1);
				}else{
					$endd=$_POST['enddate'];
				}
				$this->_view['startd']=$_POST['startdate'];
				$this->_view['endd']=$endd;
				$this->_view['logstr']=$_POST['log'];
				$addconds="datetime >='".$_POST['startdate']."' and datetime<='".$endd."'";

				if($_POST['log']!=''){
					
					$addconds.=" and log like '%".iconv("UTF-8","GB2312",$_POST['log'])."%' ";
				}
				//echo $this->my_date_add('d',$_POST['enddate'],1);
				//echo $addconds;
				//dump($_POST);
		}

		if($this->_context->startd !='' && $this->_context->endd !=''){
				$this->_view['startd']=$this->_context->startd;
				$this->_view['endd']=$this->_context->endd;
				$addconds="datetime >='".$this->_context->startd."' and datetime<='".$this->_context->endd."'";


		}

		if($this->_context->logstr!=''){
			$this->_view['logstr']=$this->_context->logstr;
			$addconds.=" and log like '%".iconv("UTF-8","GB2312",$this->_view['logstr'])."%' ";
		}
		//dump($addconds);
		// 设置页面列表数目
		$pagesize = 10;
		// 获取当前页数，通常为get方式参数
		$page = $this->_context->page;
		$this->_view['pageno']=$page;
		$select = Serverlog::find($addconds)->limitPage($page, $pagesize);
		$this->_view['pager'] = $select->getPagination();
		$this->_view['group'] = $select->order("id desc")->getAll();
		//echo $this->_view['pager']['record_count'];
		if($this->_view['pager']['record_count']>0 && $page=='')
			$page=1;

		$pagediv = new Helper_JsPager($this->_view['pager']['record_count'], $this->_view['pager']['page_size'], 9, $page, 'opgopage(\'default|serverlog|','\')',5);
		$this->_view['pagestr']= $pagediv->show();

		
	}

	/**********/
	function actionEventalarm(){
		
		if(!$this->_app->currentUserRoles())
			return $this->_redirect(url('default/login'));

		$show_box['title'] = '系统日志报警';
		Operate::everyOperate($_SESSION['acl_task_userdata']['account'],'',$_SESSION['acl_task_userdata']['group_name'],''.$show_box['title'],'');
		$this->_view['title'] = '首页-系统日志报警';
		$addconds='';
		$this->_view['startd']='';
		$this->_view['endd']='';

		//search
		$this->_view['form'] = new Form_OpSearch(url('default::default/eventalarm'));
		$this->_view['form']->_subject = '按日期查询　';
		//$this->_view['title'] = '首页-当前状态';
		$this->_view['nowuser']= $_SESSION['acl_task_userdata']['account'];
		$this->_view['nowgroup']= $_SESSION['acl_task_userdata']['group_name'];

		if ($this->_context->isPOST())
		{
				if($_POST['startdate']==$_POST['enddate']){
					$endd=$this->my_date_add('d',$_POST['enddate'],1);
				}else{
					$endd=$_POST['enddate'];
				}
				$this->_view['startd']=$_POST['startdate'];
				$this->_view['endd']=$endd;
				$addconds="time >='".$_POST['startdate']."' and time<='".$endd."'";
				//echo $this->my_date_add('d',$_POST['enddate'],1);
				//echo $addconds;
				//dump($_POST);
		}

		if(($this->_context->startd !='undefined' && $this->_context->endd !='undefined') && ($this->_context->startd !='' && $this->_context->endd !='')){
				$this->_view['startd']=$this->_context->startd;
				$this->_view['endd']=$this->_context->endd;
				$addconds="time >='".$this->_context->startd."' and time<='".$this->_context->endd."'";
		}
		
		// 设置页面列表数目
		$pagesize = 10;
		// 获取当前页数，通常为get方式参数
		$page = $this->_context->page;
		$this->_view['pageno']=$page;
		$select = EventAlarm::find($addconds)->limitPage($page, $pagesize);
		$this->_view['pager'] = $select->getPagination();
		$this->_view['group'] = $select->order("id desc")->getAll();

		if($this->_view['pager']['record_count']>0 && $page=='')
			$page=1;
		$this->_view['countno'] = $this->_view['pager']['page_size']*($page-1)+1;
		//echo $this->_view['pager']['record_count'];
		

		$pagediv = new Helper_JsPager($this->_view['pager']['record_count'], $this->_view['pager']['page_size'], 9, $page, 'opgopage(\'default|eventalarm|','\')',5);
		$this->_view['pagestr']= $pagediv->show();

		
	}

	/**********/
	function actionAlarmtable(){
		
		if(!$this->_app->currentUserRoles())
			return $this->_redirect(url('default/login'));
		
		$show_box['title'] = '行为日志报警';
		Operate::everyOperate($_SESSION['acl_task_userdata']['account'],'',$_SESSION['acl_task_userdata']['group_name'],''.$show_box['title'],'');
		$this->_view['title'] = '首页-行为日志报警';
		$addconds='';
		$this->_view['startd']='';
		$this->_view['endd']='';

		//search
		$this->_view['form'] = new Form_OpSearch(url('default::default/alarmtable'));
		$this->_view['form']->_subject = '按日期查询　';
		//$this->_view['title'] = '首页-当前状态';
		$this->_view['nowuser']= $_SESSION['acl_task_userdata']['account'];
		$this->_view['nowgroup']= $_SESSION['acl_task_userdata']['group_name'];

		if ($this->_context->isPOST())
		{
				if($_POST['startdate']==$_POST['enddate']){
					$endd=$this->my_date_add('d',$_POST['enddate'],1);
				}else{
					$endd=$_POST['enddate'];
				}
				$this->_view['startd']=$_POST['startdate'];
				$this->_view['endd']=$endd;
				$addconds="date >='".$_POST['startdate']."' and date<'".$endd."'";
				//echo $this->my_date_add('d',$_POST['enddate'],1);
				//echo $addconds;
				//dump($_POST);
		}

		if(($this->_context->startd !='undefined' && $this->_context->endd !='undefined') && ($this->_context->startd !='' && $this->_context->endd !='')){
				$this->_view['startd']=$this->_context->startd;
				$this->_view['endd']=$this->_context->endd;
				$addconds="date >='".$this->_context->startd."' and date<='".$this->_context->endd."'";
		}
		
		// 设置页面列表数目
		$pagesize = 10;
		// 获取当前页数，通常为get方式参数
		$page = $this->_context->page;
		$this->_view['pageno']=$page;
		$select = AlarmTable::find($addconds)->limitPage($page, $pagesize);
		$this->_view['pager'] = $select->getPagination();
		$this->_view['group'] = $select->order("id desc")->getAll();
		if($this->_view['pager']['record_count']>0 && $page=='')
			$page=1;
		$this->_view['countno'] = $this->_view['pager']['page_size']*($page-1)+1;
		//echo $this->_view['pager']['record_count'];
		

		$pagediv = new Helper_JsPager($this->_view['pager']['record_count'], $this->_view['pager']['page_size'], 9, $page, 'opgopage(\'default|alarmtable|','\')',5);
		$this->_view['pagestr']= $pagediv->show();

		
	}

	/**********/
	function actionSystemInfo(){
		
		if(!$this->_app->currentUserRoles())
			return $this->_redirect(url('default/login'));

		$show_box['title'] = '连接状态';
		Operate::everyOperate($_SESSION['acl_task_userdata']['account'],'',$_SESSION['acl_task_userdata']['group_name'],''.$show_box['title'],'');
		$this->_view['title'] = '连接状态';
		$addconds='';
		$this->_view['startd']='';
		$this->_view['endd']='';
		$this->_view['countno'] = 1;
		
		//search
		//$this->_view['form'] = new Form_OpSearch(url('default::default/systeminfo'));
		//$this->_view['form']->_subject = '按日期查询';
		//$this->_view['title'] = '首页-当前状态';
		$this->_view['nowuser']= $_SESSION['acl_task_userdata']['account'];
		$this->_view['nowgroup']= $_SESSION['acl_task_userdata']['group_name'];
		//INTERVAL 30 SECOND改为INTERVAL 30 SECOND
		$addconds='(systeminfo.`lasttime` >= DATE_sub( NOW( ) , INTERVAL 30 SECOND) AND systeminfo.`lasttime` < DATE_ADD( NOW( ) , INTERVAL 30 SECOND))';
		
		// 设置页面列表数目
		$pagesize = 10;
		// 获取当前页数，通常为get方式参数
		$page = $this->_context->page;
		$this->_view['pageno']=$page;
		$select = SystemInfo::find($addconds)->limitPage($page, $pagesize);
		$this->_view['pager'] = $select->getPagination();
		$this->_view['group'] = $select->order("macaddress desc")->getAll();
		//echo $this->_view['pager']['record_count'];
		if($this->_view['pager']['record_count']>0 && $page=='')
			$page=1;

		$pagediv = new Helper_JsPager($this->_view['pager']['record_count'], $this->_view['pager']['page_size'], 9, $page, 'opgopage(\'default|systemInfo|','\')',5);
		$this->_view['pagestr']= $pagediv->show();
	}

    /* ******************************************************************** */
    function actionIndex()
    {
        $show_box['title'] = '系统信息';
		$show_pp='';
		$this->_view['show_pp']='';
        if ($this->_app->currentUserRoles())
        {
            Operate::everyOperate($_SESSION['acl_task_userdata']['account'],'',$_SESSION['acl_task_userdata']['group_name'],''.$show_box['title'],'');
			
			// 如果已经登录，就转到任务列表页面
            $show_box['ncArrs'] = Helper_Permissions::getNamespaceControllerArrs();
            $this->_view['show_box'] = $show_box;
			//dump($_SESSION['acl_task_userdata']['roles']);
//SELECT * FROM `sys_acl_roles`
			$table =QDB::getConn();
			$ridnum=0;

			$allrs=explode(',',$_SESSION['acl_task_userdata']['roles']);
			
			$sql="SELECT role_id FROM `sys_acl_roles` where rolename='".$allrs[0]."'";
			//$sql="SELECT role_id FROM `sys_acl_roles` where rolename='".$_SESSION['acl_task_userdata']['roles']."'";
			//$sql = "SELECT `sys_acl_permissions`.* FROM `sys_acl_roles_have_permissions` left join `sys_acl_permissions` on `sys_acl_permissions`.permission_id = `sys_acl_roles_have_permissions`.permission_id where `sys_acl_roles_have_permissions`.role_id=6";
			$handel =$table->execute($sql);
			$temp = $handel->fetchAll();
			foreach ($temp as $s){
				$ridnum =$s['role_id'];
			}

			if($ridnum!=0){
				
				//读取可用的权限
				if($ridnum!='1'){
					$sql = "SELECT `sys_acl_permissions`.* FROM `sys_acl_roles_have_permissions` left join `sys_acl_permissions` on `sys_acl_permissions`.permission_id = `sys_acl_roles_have_permissions`.permission_id where `sys_acl_roles_have_permissions`.role_id=".$ridnum;
				}else{
					$sql = "SELECT `sys_acl_permissions`.* FROM `sys_acl_permissions`";
				}
				$ppallhave = array();
				$ppalls = array();
				$handel =$table->execute($sql);
				$temp = $handel->fetchAll();
				foreach ($temp as $s){
					if(!isset($ppallhave[$s['permission_id']]['ns'])){
					$ppallhave[$s['permission_id']]['ns']=$s['namespace'];
					$ppallhave[$s['permission_id']]['co']=$s['controller'];
					$ppallhave[$s['permission_id']]['ac']=$s['action'];
					$ppallhave[$s['permission_id']]['na']=$s['aliasname'];
					$ppalls[$s['controller']]='1';
					$ppalls[$s['controller'].'_'.$s['action']]=$s['aliasname'];
					}

					
					//echo $s['namespace'];
				}
				//dump($ppalls);
				$allbars='';
				$allmconfig=Q::ini('appini/menu_configs');
				$barsfirst=explode('|', $allmconfig['FIRST']);
				$barslinks=explode('|', $allmconfig['ACTIONAME']);
				for($o=0;$o<count($barsfirst);$o++){
					$relinkstr='';
					$relinks=explode(':', $barslinks[$o]);

					if(count($relinks)==3){
							$actionstr=$relinks[0];$actionstr1=$relinks[1];
							$relinkstr='index.php?action=framework&control='.$relinks[0].'&acts='.$relinks[1].'|'.$relinks[2].'';
						}else{
							$actionstr=$relinks[1];$actionstr1=$relinks[2];
							$relinkstr='index.php?action=framework&space='.$relinks[0].'&control='.$relinks[1].'&acts='.$relinks[2].'|'.$relinks[3].'';
						}
					
					if(isset($ppalls[$actionstr])){

						$groupbars='';

						if(isset($ppalls[$actionstr.'_'.$actionstr1])){

							if(isset($allmconfig[$actionstr.'items'])){
								$seclinks=explode(':', $allmconfig[$actionstr.'items']);
								$seclinkstr=explode('|', $seclinks[0]);
								
								$groupbars='<ul id="iul_'.$actionstr.'">';
								for($k=0;$k<count($seclinkstr);$k++){
									if(isset($ppalls[$actionstr.'_'.$seclinkstr[$k]])){
									
										$groupbars.='<li><a id="m_'.$seclinkstr[$k].'" class="returnFalse" href="index.php?action=framework&control='.$actionstr.'&acts='.$seclinkstr[$k].'|'.$seclinks[1].'">'.$ppalls[$actionstr.'_'.$seclinkstr[$k]].'</a></li>';

									}
								}

								$groupbars.='</ul>';
							}

							
							$allbars.='<li><a id="i_'.$actionstr.'" class="returnFalse" href="'.$relinkstr.'">'.$barsfirst[$o].'</a>'.$groupbars.'</li>';
						}

					}
				}
				//缓存菜单权限
				// 尝试读取缓存的数据
				$cache_id='menuitem';
				//dump($allbars);
				//dump(Q::cache($cache_id));
				if (!($allbars == Q::cache($cache_id))) {
					//$data = $allbars; // 构造数据

					// 指定缓存策略
					$policy = array(
						'life_time' => 86400, // 缓存有效时间为 1800 秒
						'serializ' => true, // 自动将数据序列化后再缓存
					);
					// 将数据缓存起来
					Q::writeCache($cache_id, $allbars, $policy);
				}
				
				//dump(Q::cache($cache_id));
				//$this->_view['show_pp'] = $allbars;
				$this->_view['show_pp'] = Q::cache($cache_id);

				//dump($allmconfig['FIRST']);
				//dump($ppallhave);
				//dump(Q::ini('appini/menu_configs'));
			}
        }
        else
        {
            // 未登录则转到登录页面
            return $this->_redirect(url('default/login'));
        }
    }

	/* ******************************************************************** */
    function actionIndexpart()
    {
        $show_box['title'] = '管理首页';
        if ($this->_app->currentUserRoles())
        {
            Operate::everyOperate($_SESSION['acl_task_userdata']['account'],'',$_SESSION['acl_task_userdata']['group_name'],''.$show_box['title'],'');
			
			// 如果已经登录，就转到任务列表页面
            $show_box['ncArrs'] = Helper_Permissions::getNamespaceControllerArrs();
            $this->_view['show_box'] = $show_box;
        }
        else
        {
            // 未登录则转到登录页面
            return $this->_redirect(url('default/login'));
        }
    }

	function actionImgcode() {
		$pagediv = new Helper_ImCode();
		$this->_view['stxt']= $pagediv->image();
	}
    /* ******************************************************************** */
    function actionLogin()
    {
        //echo 'df';
		$this->_view['title'] = '登录系统';
		$this->_view['form'] = new Form_UserLogin(url('default/login'));
		//$ati= new Helper_ImgCode();
		//$this->_view['stxt']=$ati->clear(); 
		$imgcode = new Helper_ImCode();
		//$this->_view['stxt']= $pagediv->image();
		//        $this->_view['form'] = new Form_UserLogin(url('default/login'));
		//        $this->_view['form']->_subject = $show_box['title'];
        if ($this->_context->isPOST() && $this->_view['form']->validate($_POST))
        {
            try
            {
                //Operate::everyOperate($_SESSION['acl_task_userdata']['account'],'',$_SESSION['acl_task_userdata']['group_name'],''.$this->_view['title'].'-成功','');
				
				//if($imgcode->check($_POST['codes'])){
				
				// 使用 acluser 插件的 validateLogin() 方法验证登录并取得有效的 user 对象
                $user = Admin_Users::meta()->validateLogin($this->_view['form']['account']->value, $this->_view['form']['password']->value);
                $userInfo = $user->aclData();
                if ( $userInfo['is_locked'] ){ 
                //判断用户是否被锁定
                    $this->_view['form']['account']->invalidate("被锁定的用户，请与管理员联系。");
                }else{
                    // 获取用户权限组，并将登录用户的信息存入 SESSION，以便应用程序记住用户的登录状态
                        //用扩展的函数获取用户权限，直接判定用户的权限，并根据用户的状态进行角色转换
                    $Helper_Acl = new Helper_Acl;
                    $user_AllRoles = $Helper_Acl->UserAclRoles($userInfo['user_id']);
                    $this->_app->changeCurrentUser($user->aclData(), $user_AllRoles);

					//CACHE RIGHT
					//区分用户
					 $os = array("admin");

					 $aroles = array();
					 $aroles =split('[,]', $_SESSION['acl_task_userdata']['roles']);
					//dump($aroles);
					$isrightp=false;
					 foreach ($aroles as $value){
						if (in_array(strtolower($value), $os)) {
							$isrightp=true;
							//echo 'opip';
							break;
							//echo "Got Irix";
						}
					 }
					 //echo $isrightp;
					 //echo $_SESSION['acl_task_userdata']['user_id'];
					 $adming='';
					 if(!$isrightp){
						$havegroups = array();
						$select1 = Groupuser::find('userid = ?',$_SESSION['acl_task_userdata']['user_id'])->getAll();
						foreach ($select1 as $av){
							array_push($havegroups,$av->groupid);
							//echo $av->groupid;
						}
						$addconds='';
						//dump($havegroups);
						$hgs=count($havegroups);
						$this->_view['adming']=$hgs;
						if($hgs>0){
							$groupno=''.join(",",$havegroups).'';
						}else{
							$groupno='0';
						}
					 }else{
						
						$this->_view['adming']=-1;
					 }
					 //END
					$cache_id='rightitem';
					//dump($allbars);
					//dump(Q::cache($cache_id));
					if (!($groupno == Q::cache($cache_id))) {
						//$data = $allbars; // 构造数据

						// 指定缓存策略
						$policy = array(
							'life_time' => 86400, // 缓存有效时间为 1800 秒
							'serializ' => true, // 自动将数据序列化后再缓存
						);
						// 将数据缓存起来
						Q::writeCache($cache_id, $groupno, $policy);
					}
					
					//dump(Q::cache($cache_id));
					//$this->_view['show_pp'] = $allbars;
					$this->_view['show_gg'] = Q::cache($cache_id);
				 //CACHE END
					//CACHE END
                    // 登录成功后，重定向浏览器
//                    return $this->_redirectMessage(
//                        '登录成功', 
//                        '您已经成功登录。登录后可以创建和修改任务列表。',
//                        url('default/index'));
					return $this->_redirect(url('default/index'));
                }

//				}else{
//					$this->_view['form']['password']->invalidate("验证码不正确");
//				}
            }
            catch (AclUser_UsernameNotFoundException $ex)
            {
                //$this->_view['form']['account']->invalidate("您输入的用户名 {$this->_view['form']['account']->value} 不存在");
				$this->_view['form']['account']->invalidate("用户不存在或者密码错误");

				//Operate::everyOperate($_SESSION['acl_task_userdata']['account'],'',$_SESSION['acl_task_userdata']['group_name'],''.$this->_view['title'].'-失败','');
				//if(!$imgcode->check($_POST['codes']))
				//$this->_view['form']['password']->invalidate("验证码不正确");
            }
            catch (AclUser_WrongPasswordException $ex)
            {
                $this->_view['form']['password']->invalidate("用户不存在或者密码错误");

				//Operate::everyOperate($_SESSION['acl_task_userdata']['account'],'',$_SESSION['acl_task_userdata']['group_name'],''.$this->_view['title'].'-失败','');
            }
        }
        //$this->_view['show_box'] = $show_box;
        //$this->_viewname = 'index';
    }

    /* ******************************************************************** */
    function actionLogout()
    {
        $show_box['title'] = '注销系统';

		Operate::everyOperate($_SESSION['acl_task_userdata']['account'],'',$_SESSION['acl_task_userdata']['group_name'],''.$show_box['title'],'');
        $this->_app->cleanCurrentUser();
		//清空缓存
		Q::cleanCache('menuitem');
        //return $this->_redirectMessage(
        //    '退出登录', 
        //    '您已经退出了登录状态，谢谢使用。',
        //    url('default/index'));
		return $this->_redirect(url('default/index'));
		
    }

    /* ******************************************************************** */
    function actionRegister()
    {
        $show_box['title'] = '会员注册';
        // 构造表单对象
        $form = new Form_UserReg(url('default/register'));
        if ($this->_context->isPOST() && $form->validate($_POST))
        {
            // 是 POST 提交，并且表单验证通过
            try
            {
                // 创建 User 对象并保存
                $user = new Admin_Users($form->values());
                // 自动添加用户的默认组
                $user->roles[] = Admin_Roles::find('rolename = ?', 'UNCHECKED')->query();
                $user->save();
                // 成功后重定向到登录页面
                return $this->_redirectMessage(
                    '注册新用户', 
                    '您的新帐户已经注册成功，请输入用户名和密码登录。',
                    url('default/login'));
            }
            catch (AclUser_DuplicateUsernameException $ex)
            {
                // 捕获 AclUser_DuplicateUsernameException 异常，在表单中指出用户名存在重复问题
                $form['account']->invalidate("您要注册的帐号 {$user->account} 已经存在了");
            }
        }
        // 将表单对象传递给视图
        $this->_view['form'] = $form;
        $this->_viewname = 'form';
    }

    /* ******************************************************************** */
    function actionChangePasswd()
    {
       if(!$this->_app->currentUserRoles())
			return $this->_redirect(url('default/login'));
		
		$this->_view['title'] = '修改密码　';

		Operate::everyOperate($_SESSION['acl_task_userdata']['account'],'',$_SESSION['acl_task_userdata']['group_name'],''.$this->_view['title'],'');

        $form = new Form_ChangePasswd(url('default/changepasswd'));

        if ($this->_context->isPOST() && $form->validate($_POST))
        {
            try
            {
                // 取得当前用户的信息
                $current_user = $this->_app->currentUser();
                // 修改密码
                Admin_Users::meta()->changePassword($current_user['account'], $form['new_password']->value, $form['old_password']->value);
                // 重定向浏览器
                return $this->_redirectMessage(
                    '修改密码成功', 
                    '您的登录密码已经成功修改，下次登录请使用新密码。',
                    url('default/index'));
            }
            catch (AclUser_WrongPasswordException $ex)
            {
                $form['old_password']->invalidate('您输入的现用密码不正确');
            }
        }

        $this->_view['form'] = $form;
        $this->_viewname = 'pwdform';
    }

    function my_date_add($opt,$time,$num){
		$year = date('Y',strtotime($time));
		$month = date('m',strtotime($time));
		$day = date('d',strtotime($time));
		$last_day = date('t',strtotime($time));
		if($opt=='y'){
			$year = $year + $num;
		}else if($opt=='d'){
			$day = $day + $num;
		}else if($opt=='m'){
			$month = $month + $num;
			if($day == $last_day){//如果是最后一天的话
				$day = date('t',strtotime("{$year}-{$month}-01"));
			}
		} else {
			return $time;
		}
		return date("Y-m-d", mktime(0, 0, 0, $month, $day, $year));
	}
}